<script setup lang="ts">
import { loginAPI } from '@/apis/user';
import { FORMDATA_KEY } from '@/constants';
import { useUserStore } from '@/stores/user';
import { ElMessage, type FormRules } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';
import { useRouter } from 'vue-router';

// 1. 数据绑定
const formData = ref({
  username: '',
  password: '',
  remember: false
})

// 2. 数据校验
const formRef = ref()
const formRules = reactive<FormRules>({
  username: [
    {required: true, message: '用户名不能为空', trigger: 'blur'}
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' }
  ]
})

// 3. 登录
const store = useUserStore()
const router = useRouter()
const onLogin = ()=>{
  // 3.1 表单整体校验
  formRef.value.validate(async (valid: boolean, fields: object)=>{
    if(valid){
      // 3.2 调用登录接口
      const res = await loginAPI({
        username: formData.value.username,
        password: formData.value.password
      })

      // 3.5 登录成功之后, 判断是否把用户名和密码存储到本地 
      if(formData.value.remember){
        localStorage.setItem(FORMDATA_KEY, JSON.stringify(formData.value))
      } else {
        localStorage.removeItem(FORMDATA_KEY)
      }

      ElMessage({
        message: '登录成功',
        type: 'success'
      })
      // 3.3 把token存储到pinia(持久化)
      store.setToken(res.data.token)

      // 3.4 跳转到主页面
      router.replace('/')
    } 
  })
}

// 4. 从本地获取用户名和密码
onMounted(()=>{
  const jsonStr = localStorage.getItem(FORMDATA_KEY)
  if(jsonStr !== null){
     formData.value = JSON.parse(jsonStr)
  }
})
</script>

<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="formRef" :model="formData" :rules="formRules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" clearable />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" show-password />
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="formData.remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="onLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login_body {
  display: flex;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url("@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}

.box {
  margin: 200px 10% 0;
  flex: 1;

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }

  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }

    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }

    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}

.login_btn {
  width: 100%;
}
</style>